﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;

namespace CameraAcq
{
    public delegate void _imageOut(List<byte[]> imageByte);
    [Serializable]
     public class CameraBase
    {
        public static int m_LastDeviceID = 0;
        private string _DeviceID;
        private DeviceType _DeviceType;
        private string _UniqueLabel;
        private string _DevDirExt;
        private string _SerialNo;//序列号
        private string _camName;//相机名字
        private string _UserDefineName;//用户定义名字
        private string _ManufacturerName;//相机厂商名字
        private bool _bConnected = false;//设备当前状态
        private bool _bGrabbed = false;//设备是否已采集
        private List<byte[]> _imageByteList = new List<byte[]>();//相机输出byte[]格式的图像
        private int _imageNumber = 0;//采集的图像总数量
        [NonSerialized]
        //private HImage _Image = new HImageExt();
        private bool _IsNewImage;
        [NonSerialized]
        public AutoResetEvent eventWait = new AutoResetEvent(false);//采集信号
        [NonSerialized]
        public _imageOut imageOut;
        /// <summary>
        /// sensor 类型 是线扫还是面阵相机
        /// </summary>
        public SensorType m_SensorType;

        /// <summary>
        /// 设备ID,自己编号
        /// </summary>
        public string m_DeviceID
        {
            set { _DeviceID = value; }
            get { return _DeviceID; }
        }

        /// <summary>
        /// 设备类型(品牌)
        /// </summary>
        public DeviceType m_DeviceType
        {
            set { _DeviceType = value; }
            get { return _DeviceType; }
        }

        /// <summary>
        /// 唯一标示 eg:设备连接ID/文件夹路径/设备有效识别的ID
        /// </summary>
        public string m_UniqueLabel
        {
            set { _UniqueLabel = value; }
            get { return _UniqueLabel; }
        }

        /// <summary>
        /// 设备扩展标识，大部分时间用不上
        /// </summary>
        public string m_DevDirExt
        {
            set { _DevDirExt = value; }
            get { return _DevDirExt; }
        }

        /// <summary>
        /// 设备初始期许状态是否连接
        /// </summary>
        public bool m_bConnected
        {
            set { _bConnected = value; }
            get { return _bConnected; }
        }
        public bool m_bGrabbed
        {
            set { _bGrabbed = value; }
            get { return _bGrabbed; }
        }
        /// <summary>
        /// 设备内部编号(序列号)
        /// </summary>
        public string m_SerialNo
        {
            set { _SerialNo = value; }
            get { return _SerialNo; }
        }
        public string m_camName
        {
            set { _camName = value; }
            get { return _camName; }
        }
        public string m_UserDefineName
        {
            set { _UserDefineName = value; }
            get { return _UserDefineName; }
        }
        public string m_manufacturerName
        {
            set { _ManufacturerName = value; }
            get { return _ManufacturerName; }
        }
        //public HImage m_Image
        //{
        //    set { _Image = value; }
        //    get { return _Image; }
        //}
        /// <summary>
        /// 是不是最新采集的图像
        /// </summary>
        public bool m_IsNewImage
        {
            set { _IsNewImage = value; }
            get { return _IsNewImage; }
        }

        /// <summary>
        /// X方向像素当量
        /// </summary>
        public double ScaleX { get; set; }

        /// <summary>
        /// Y方向像素当量
        /// </summary>
        public double ScaleY { get; set; }

        public string m_SettingPath
        {
            get { return Environment.CurrentDirectory + "\\" + m_DeviceID + "_Setting"; }
        }

        public  List<byte[]> m_imageByteList
        {
            get { return _imageByteList; }
            set { _imageByteList = value; }
        }

        public int m_imageNumber
        {
            get { return _imageNumber; }
            set { _imageNumber = value; }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_DeviceType">初始化时指定设备类型</param>
        public CameraBase(DeviceType _DeviceType)
        {
            this._DeviceType = _DeviceType;
            m_LastDeviceID++;
            _DeviceID = "Dev" + m_LastDeviceID.ToString();
        }
        public CameraBase(string  camSerial)
        {
            this._SerialNo = camSerial;
        }

        public virtual void AcqDeviceList(ref List<CamInfo> cameralist)
        {

        }
        public virtual void AcqDeviceList()
        {

        }
        //public virtual void CreateDevice(ref CamInfo _CamInfo);//创建设备
        public virtual bool ConnectDev()//建立设备连接
        {
            return true;
        }
        public virtual bool DisConnectDev() //断开设备连接
        {
            return true;
        }

        public virtual bool StartGrab() //开始采集
        {
            return true;
        }
        public virtual bool stopGrab() //停止采集
        {
            return true;
        }
        /// <summary>
        /// 抓捕图像，软触发
        /// </summary>
        /// <param name="softTrigger">是否软件触发采图</param>
        /// <returns>采集是否成功</returns>
        public virtual bool CaptureImage(bool softTrigger)
        {
            return true;
        }

        public virtual void SetParamter(float exposure,float gain,float frame)//曝光、增益、帧率设置
        {

        }

        public virtual void GetParameter(ref float expoure,ref float gain,ref float frame)//获取曝光、增益、帧率参数
        {

        }

        public virtual void SaveSetting(string filePath)
        {

        }

        public virtual void LoadSetting(string filePath)
        {

        }
        /// <summary>
        /// 设置曝光时间
        /// </summary>
        /// <param name="dValue">曝光时间</param>
        /// <returns></returns>
        public virtual bool setExposureTime(float dValue)
        {
            return true;
        }

        /// <summary>
        /// 设置触发模式
        /// </summary>
        /// <param name="mode">触发模式</param>
        /// <returns>成功返回True</returns>
        public virtual bool SetTriggerMode(TriggerMode mode)
        {
            return true;
        }
        //public virtual void ReleaseDevice();//释放设备
        /// <summary>
        /// 设置帧率
        /// </summary>
        /// <param name="FrameRate">帧率</param>
        public virtual void SetFrameRate(double FrameRate)
        {
        }
        [OnDeserializing()]
        internal void OnDeSerializingMethod(StreamingContext context)
        {
            //_Image = new HImage();
            eventWait = new AutoResetEvent(false);//采集信号
        }
    }
}
